package com.wzw.expression.record.proxy;

import com.wzw.expression.expression.source.OperationSource;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.lang.NonNull;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * IRecord 切点
 *
 * @author Wangzhiwen
 */
public class IRecordMethodPointcut extends StaticMethodMatcherPointcut implements Serializable {

    private OperationSource recordOperationSource;

    public IRecordMethodPointcut() {
        setClassFilter(new IRecordOperationSourceClassFilter());
    }

    public void setRecordOperationSource(OperationSource recordOperationSource) {
        this.recordOperationSource = recordOperationSource;
    }

    @Override
    public boolean matches(@NonNull Method method, @NonNull Class<?> targetClass) {
        return Objects.nonNull(recordOperationSource) && !CollectionUtils.isEmpty
                (
                        recordOperationSource.getOperations(method, targetClass)
                );
    }

    private class IRecordOperationSourceClassFilter implements ClassFilter {
        @Override
        public boolean matches(@NonNull Class<?> clazz) {
            return Objects.nonNull(recordOperationSource) && recordOperationSource.isCandidateClass(clazz);
            // return false;
        }

    }

}
